package com.dc.service.impl;

import com.dc.base.em.ErrorMessageEnum;
import com.dc.base.vo.BaseModel;
import com.dc.base.vo.BusinessException;
import com.dc.dao.ClassifyDao;
import com.dc.model.Classify;
import com.dc.service.ClassifyService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class ClassifyServiceImpl implements ClassifyService {
//    对类成员变量、方法及构造函数进行标注,完成自动装配的工作,消除get set
    @Autowired
    ClassifyDao classifyDao;
    // 分页查询分类信息(带搜索)
    public BaseModel selectPageClassify(Integer curr_page, Integer page_size, BaseModel baseModel, String classify_name,String memo) {
            // 编写业务，实现分页 limit   (mybatis分页工具)
            PageHelper.startPage(curr_page,page_size);
            // 开启分页以后需要马上接入查询语句，方便pageHelper实现分页
            List<Classify> classifys=classifyDao.selectPageClassify(classify_name,memo);
            // 在new PageInfo<>(list)的时候可以把页码、页大小、总页数等信息给pageinfo，navigate导航
            PageInfo pageInfo=new PageInfo(classifys,5);
            // 设置结果code
            baseModel.setResultCode(0);
            // 设置消息提示
            baseModel.setMessage("分页查询分类成功！");
            // 插入数据
            baseModel.setData(pageInfo);
            // 返回basemodel
            return baseModel;
    }

    // 新增分类的接口
    public BaseModel addClassify(Classify classify, BaseModel baseModel) throws Exception {
        // 判断是否本分类已经添加
        Integer classifyS = classifyDao.selectClassifyByName(classify.getClassify_name());
        if(classifyS>0){
            // 有账号不准插入
            throw new BusinessException(ErrorMessageEnum.REPEAT_CLASSIFY);
        }
        //调用新增分类的dao
        Integer result=classifyDao.insertClassify(classify);
        if(result<=0){
            throw new BusinessException(ErrorMessageEnum.ADD_USER_ERROR);
        }
        baseModel.setResultCode(0);
        baseModel.setMessage("新增分类成功！");
        return baseModel;
    }

    // 修改分类的接口
    public BaseModel updateClassify(Classify classify, BaseModel baseModel) throws Exception {
        Integer result=classifyDao.updateClassify(classify);
        if(result<=0){
            throw  new BusinessException(1008,"修改分类失败");
        }
        baseModel.setResultCode(0);
        baseModel.setMessage("修改分类成功！");
        return baseModel;
    }

    // 删除分类的接口
    public BaseModel deleteClassify(String ids, BaseModel baseModel) throws Exception {
        String[] idArry=ids.split(",");
        int result=classifyDao.deleteClassify(idArry);
        if(result<=0){
            throw new BusinessException(1009,"删除"+result+"个分类失败。");
        }
        baseModel.setResultCode(0);
        baseModel.setMessage("删除"+result+"个分类成功！");
        return baseModel;
    }

    // 根据id查询分类信息的接口
    public BaseModel selectClassifyById(Integer classify_id,BaseModel baseModel) throws Exception{
        List<Classify> classifyList = classifyDao.selectClassifyById(classify_id);
        baseModel.setResultCode(0);
        baseModel.setMessage("查询单个分类成功");
        baseModel.setData(classifyList);
        return baseModel;
    }
}
